package com.lnb.gateway.filter;

import com.lnb.gateway.constant.Constant;
import com.lnb.gateway.model.WhiteListDefine;
import com.lnb.gateway.properties.RouterProperties;
import com.lnb.gateway.properties.WhitelistProperties;
import com.lnb.gateway.util.ExchangeUtils;
import com.lnb.gateway.util.IPUtils;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author liaoningbo
 * @since 2024-01-08
 */
@Component
@Log4j2
public class IpWhiteListFilter implements GlobalFilter, Ordered {

    @Resource
    private WhitelistProperties whitelistProperties;
    @Resource
    private RouterProperties routerProperties;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("开始ip白名单校验");
        //判断是否跳过当前校验
        if (ExchangeUtils.isSkip(exchange)) {
            return chain.filter(exchange);
        }
        ServerHttpRequest request = exchange.getRequest();
        //获取接口路径
        String path = request.getPath().toString();
        //获取请求ip
        String ip = IPUtils.getIpAddress(request);
        //判断当前ip是否在白名单中，true：在，false：不在
        boolean isWhiteIp = this.isWhitelistIp(path, ip);
        //如果在白名单，后续所有校验全都跳过
        if(isWhiteIp){
            log.info("请求ip在白名单中，后续校验直接跳过");
            ExchangeUtils.setSkip(exchange);
            return chain.filter(exchange);
        }else{
            //如果不在白名单内，需要继续其他校验
            log.info("IP不在白名单内，继续执行后续的校验");
            return chain.filter(exchange);
        }
    }

    @Override
    public int getOrder() {
        return Constant.IP_FILTER_ORDER;
    }

    /**
     * <p>
     *     判断ip是否在白名单中
     * </p>
     * @author liaoningbo
     * @since 2024/1/8
     */
    public boolean isWhitelistIp(String path, String ip) {
        //根据请求path，获取对应的routeId
        String routeId = routerProperties.getIdByRequestPath(path);
        //如果routeId为空，说明该请求path匹配不到对应的路由，直接返回false
        if(StringUtils.isEmpty(routeId)){
            return false;
        }
        //获取该routeId下的ip白名单列表
        List<String> values = this.getIpWhiteListById(routeId);
        //如果没有配置ip，返回false
        if (values.size() == 0){
            return false;
        }else {
            //判断请求ip是否在白名单中
            return values.contains(ip);
        }
    }

    /**
     * <p>
     *     获取routeId下的ip白名单列表
     * </p>
     * @author liaoningbo
     * @since 2024/1/8
     */
    public List<String> getIpWhiteListById(String routeId) {
        List<String> pathWhite = new ArrayList<>();
        //获取配置文件中的ip白名单列表
        for (WhiteListDefine ipWhitelistDefine : whitelistProperties.getIpWhiteList()) {
            //如果该请求的routeId存在，需要获取routeId下的ip列表
            if (ipWhitelistDefine.getId().equals(routeId)){
                pathWhite = ipWhitelistDefine.getList();
                break;
            }
        }
        return pathWhite;
    }

}
